/**
 * 库存查询管理初始化
 */
var Instock = {
    id: "InstockTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Instock.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: true, align: 'center', valign: 'middle'},
            {title: '标识码', field: 'standardCode', visible: true, align: 'center', valign: 'middle'},
            {title: '名称', field: 'standardName', visible: true, align: 'center', valign: 'middle'},
            {title: '入库时间', field: 'standardInDate', visible: true, align: 'center', valign: 'middle'},
            {title: '出库时间', field: 'standardOutDate', visible: true, align: 'center', valign: 'middle'},
            {title: '入库数量', field: 'standardInNumber', visible: true, align: 'center', valign: 'middle'},
            {title: '出库数量', field: 'standardOutNumber', visible: true, align: 'center', valign: 'middle'},
            {title: '剩余量', field: 'standardNumber', visible: true, align: 'center', valign: 'middle'},
            {title: '出入库总数', field: 'standardFrequency', visible: true, align: 'center', valign: 'middle'},
            {title: '入库次数', field: 'standardInFrequency', visible: true, align: 'center', valign: 'middle'},
            {title: '出库次数', field: 'standardOutFrequency', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
Instock.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Instock.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加库存查询
 */
Instock.openAddInstock = function () {
    var index = layer.open({
        type: 2,
        title: '添加库存查询',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/instock/instock_add'
    });
    this.layerIndex = index;
};

/**
 * 跳转到添加入库界面
 */
Instock.openAddIn = function () {
    var index = layer.open({
        type: 2,
        title: '添加入库',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/instock/instock_addIn'
    });
    this.layerIndex = index;
};

/**
 * 跳转到添加出库界面
 */
Instock.openAddOut = function () {
    var index = layer.open({
        type: 2,
        title: '添加入库',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/instock/instock_addOut'
    });
    this.layerIndex = index;
};

/**
 * 打开查看库存查询详情
 */
Instock.openInstockDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '库存查询详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/instock/instock_update/' + Instock.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除库存查询
 */
Instock.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/instock/delete", function (data) {
            Feng.success("删除成功!");
            Instock.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("instockId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询库存查询列表（搜索框旁边的按钮）
 */
Instock.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Instock.table.refresh({query: queryData});
};

/**
 * 库存报警standardNumber==0,库存量为0
 */
Instock.lookStandardNumber = function () {
    var queryData = {};
    queryData['condition'] = 0;
    Instock.table.refresh({query: queryData});
};

/**
 * 查看以往记录
 */
Instock.lookStandardCode = function () {
    var queryData = {};
    queryData['condition'] = $("#standardCode").val();
    Instock.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Instock.initColumn();
    var table = new BSTable(Instock.id, "/instock/list", defaultColunms);
    table.setPaginationType("client");
    Instock.table = table.init();
});
